package com.htfyun.roobofactory.factorytest;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.htfyun.roobofactory.BaseActivity;
import com.htfyun.roobofactory.R;
import com.htfyun.roobofactory.ViewDialogFragment;

/**
 * Created by sst on 2018/7/7.
 */

public class LcdTestActivity extends BaseActivity {

    private View container;
    private View layoutBtn;

    private Button btnPass;
    private Button btnFail;

    private boolean canExist = false;
    private int currentColorIndex;

    private Context mContext;

    private final int[] LCD_SHOW_COLORS = {
            Color.WHITE, Color.RED, Color.GREEN,
            Color.BLUE, Color.YELLOW, Color.CYAN,
            Color.MAGENTA, Color.BLACK
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_factorytest_lcd);

        mContext = this;

        container = findViewById(R.id.container);
        layoutBtn = findViewById(R.id.layoutBtn);
        layoutBtn.setVisibility(View.GONE);

        btnPass = (Button) findViewById(R.id.btnPass);
        btnFail = (Button) findViewById(R.id.btnFail);
        btnPass.setOnClickListener(onPassOrFailClickListener);
        btnFail.setOnClickListener(onPassOrFailClickListener);

        currentColorIndex = 0;
        container.setBackgroundColor(LCD_SHOW_COLORS[currentColorIndex]);
        container.setOnClickListener(onClickListener);
//        container.setOnLongClickListener(longClickListener);

    }

    @Override
    protected void onResume() {
        super.onResume();
        Toast.makeText(this, R.string.test_lcd_toast, Toast.LENGTH_SHORT).show();
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (canExist) {
                return;
            }

            currentColorIndex ++;

            if (currentColorIndex >= LCD_SHOW_COLORS.length) {
                currentColorIndex = 0;

                canExist = true;

                layoutBtn.setVisibility(View.VISIBLE);

            }

            container.setBackgroundColor(LCD_SHOW_COLORS[currentColorIndex]);

        }
    };

    private View.OnLongClickListener longClickListener =
            new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    if (!canExist) {
                        return false;
                    }

                    new ViewDialogFragment().show(getFragmentManager(),
                            R.string.test_lcd,
                            R.string.item_exit_test,
                            new ViewDialogFragment.Callback() {
                                @Override
                                public void onClick() {

                                    setResult(RESULT_OK);
                                    ActivityCompat.finishAfterTransition(getActivity());

                                }
                            },
                            new ViewDialogFragment.Callback() {
                                @Override
                                public void onClick() {

                                    setResult(RESULT_CANCELED);
                                    ActivityCompat.finishAfterTransition(getActivity());
                                }
                            });
                    return true;
                }
            };


    private View.OnClickListener onPassOrFailClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btnPass) {

                setResult(RESULT_OK);
                ActivityCompat.finishAfterTransition(getActivity());

            } else if (v == btnFail) {

                setResult(RESULT_CANCELED);
                ActivityCompat.finishAfterTransition(getActivity());

            }
        }
    };

}
